package com.achun.getcouponleague.ui.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.achun.getcouponleague.Bean.CategoriesContent;
import com.achun.getcouponleague.Bean.SelectCategories;
import com.achun.getcouponleague.Bean.SelectContent;
import com.achun.getcouponleague.R;
import com.achun.getcouponleague.base.BaseFragment;
import com.achun.getcouponleague.presenter.impl.SelectorPresenter;
import com.achun.getcouponleague.presenter.impl.TiketPresenter;
import com.achun.getcouponleague.ui.activity.TiketActivity;
import com.achun.getcouponleague.ui.adapter.SelectCategoryAdapter;
import com.achun.getcouponleague.ui.adapter.SelectContentAdapter;
import com.achun.getcouponleague.utils.PresenterManager;
import com.achun.getcouponleague.utils.UIutils;
import com.achun.getcouponleague.view.ISelectorCallBack;

import java.util.List;

import butterknife.BindView;

public class Select_Fragment extends BaseFragment implements ISelectorCallBack {

    private SelectorPresenter selectorPresenter;
    @BindView(R.id.category_list)
    public RecyclerView category_list;
    @BindView(R.id.content_by_id_list)
    public RecyclerView content_list;
    private SelectCategoryAdapter selectCategoryAdapter;
    private SelectContentAdapter selectContentAdapter;
    private TiketPresenter tiketPresenter;

    @Override
    public View loadSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_select,container,false);
    }

    @Override
    protected void initPresenter() {
        selectorPresenter = PresenterManager.getInstance().getSelectorPresenter();
        tiketPresenter = PresenterManager.getInstance().getTiketPresenter();
        selectorPresenter.registerCallback(this);
        //读取分类数据
        selectorPresenter.getSelectCategories();
    }
    //处理跳转事件
    private void handleclick(SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean item){
        String coupon_share_url = "https:"+item.getCoupon_share_url();
        String title = item.getTitle();
        String cover = item.getPict_url();
        tiketPresenter.createTiketCode(coupon_share_url,title,cover);
        Intent intent = new Intent(getContext(), TiketActivity.class);
        startActivity(intent);
    }
    @Override
    protected void initListener() {
        //item点击事件
        selectContentAdapter.setOnItemClickListener(new SelectContentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean bean) {
                handleclick(bean);
            }
        });
        //精选分类点击事件
        selectCategoryAdapter.setOnLeftListClickListenter(new SelectCategoryAdapter.OnLeftListClickListenter() {
            @Override
            public void onclick(SelectCategories.DataBean bean) {
                selectorPresenter.getContentById(bean.getFavorites_id());
            }
        });
    }

    @Override
    protected void onReTry() {
//读取分类数据
        selectorPresenter.getSelectCategories();
    }

    @Override
    protected void initView(View rootview) {
        //创建适配器
        selectCategoryAdapter = new SelectCategoryAdapter();
        selectContentAdapter = new SelectContentAdapter();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getContext());
        category_list.setLayoutManager(linearLayoutManager);
        content_list.setLayoutManager(linearLayoutManager1);
        content_list.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int size = UIutils.dip2px(getContext(), 10);
                outRect.right=size;
                outRect.left=size;
                outRect.top=size;
                outRect.bottom=size;
            }
        });
        category_list.setAdapter(selectCategoryAdapter);
        content_list.setAdapter(selectContentAdapter);
    }

    @Override
    public void onLoading() {
        setViewStatus(Status.LOADING);
    }

    @Override
    public void onError() {
    setViewStatus(Status.ERROR);
    }

    @Override
    public void onEmpty() {
setViewStatus(Status.EMPTY);
    }

    @Override
    public void onLoadedCategorys(SelectCategories selectCategories) {
        setViewStatus(Status.SUCCESS);
        selectCategoryAdapter.setDatas(selectCategories);


    }

    @Override
    protected void release() {
        selectorPresenter.unRegisterCallback(this);
    }

    @Override
    public void onLoadedContentById(SelectContent selectContent) {
        if (selectContent!=null) {
            List<SelectContent.DataBean.TbkDgOptimusMaterialResponseBean.ResultListBean.MapDataBean> map_data = selectContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data();
            selectContentAdapter.setDatas(map_data);
            content_list.scrollToPosition(0);
        }
    }
}